package sample.jasperreport.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sample.jasperreport.service.JasperService;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author : dongyz8
 * @Date : 2023/11/6 9:04
 **/
@RestController
@RequestMapping("/jasper")
public class JasperController {

    @Autowired
    private JasperService jasperService;

    @RequestMapping("/getPDF")
    public void getPDF(HttpServletResponse response){
        byte[] bytes = jasperService.toPDF(response);
        OutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            outputStream.write(bytes);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if(null != outputStream){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }
}
